*=*=*=*=**=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
* purpose: construct weather variables

* author: Jonathan Colmer - j.colmer@virginia.edu

* data downloaded from http://www.wolfram-schlenker.com/dailyData.html
* (accessed 01/02/2018 0900 H)

*=*=*=*=*=**=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=**=*=*=*=*=*

*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
* Daily Data 																
*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=

** Read in raw data and construct weather variables

quietly:{
forvalues y = 1991(1)2016{
	foreach x in 1 4 5 6 8 9 10 11 12 13 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 44 45 46 47 48 49 50 51 53 54 55 56 {
		use "Raw Data/Weather Data/`y'/state`x'.dta", clear
		merge m:1 gridNumber using "Raw Data/Weather Data/linkGridnumberFIPS.dta"
		keep if _m == 3
		drop _m

		* Construct relevant temporal measures *
		egen day = group(dateNum)
		gen month = month(dateNum)
		gen year = year(dateNum)

		* Create 24hr average temperature
		egen tMean = rmean(tMax tMin)
		gen tMean2 = tMean^2
		gen tMean3 = tMean^3
		gen tMean4 = tMean^4

		gen DD_H = 0 if tMean <= 18
		replace DD_H = tMean- 18 if tMean > 18
		
		collapse (mean) tMean* DD_H tMax tMin prec, by(fips day month year) 

		save "Raw Data/Weather Data/`y'/state_`x'_`y'_daily.dta", replace
}
}
}

** Append all states so that there is one data set for each year **
forvalues y = 1991(1)2016{
	use "Raw Data/Weather Data/`y'/state_1_`y'_daily.dta", clear
	foreach x in 4 5 6 8 9 10 11 12 13 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 44 45 46 47 48 49 50 51 53 54 55 56 {
		append using "Raw Data/Weather Data/`y'/state_`x'_`y'_daily.dta"
	}
	save "Raw Data/Weather Data/`y'/all_states_`y'_daily.dta", replace
}

** Append all years so that there is one data set for all counties in the US between 1991 and 2017 **
use "Raw Data/Weather Data/1991/all_states_1991_daily.dta", clear
forvalues y = 1992(1)2016{
	append using "Raw Data/Weather Data/`y'/all_states_`y'_daily.dta"
}
duplicates drop fips day month year, force

egen dmy = group(year month day)

xtset fips dmy

*create date variable 
// day = day of year, neeed to create day of month
rename day day_original
gen day=day_original

*non-leap years
replace day=day_original-31 if month==2
replace day=day_original-59 if month==3
replace day=day_original-90 if month==4
replace day=day_original-120 if month==5
replace day=day_original-151 if month==6
replace day=day_original-181 if month==7
replace day=day_original-212 if month==8
replace day=day_original-243 if month==9
replace day=day_original-273 if month==10
replace day=day_original-304 if month==11
replace day=day_original-334 if month==12

*leap years
forvalues y=1992(4)2016 {
	replace day=day_original-60 if month==3 & year==`y'
	replace day=day_original-91 if month==4 & year==`y'
	replace day=day_original-121 if month==5 & year==`y'
	replace day=day_original-152 if month==6 & year==`y'
	replace day=day_original-182 if month==7 & year==`y'
	replace day=day_original-213 if month==8 & year==`y'
	replace day=day_original-244 if month==9 & year==`y'
	replace day=day_original-274 if month==10 & year==`y'
	replace day=day_original-305 if month==11 & year==`y'
	replace day=day_original-335 if month==12 & year==`y'
}
gen date=mdy(month,day,year)
format date %dM_d,_CY
order fips date

save "Processed Data/US_weather_1991_2016_daily.dta", replace


*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
* MONTHLY DATA 																
*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=

* Read in raw data and construct weather variables

quietly:{
	forvalues y = 1991(1)2016{
		foreach x in 1 4 5 6 8 9 10 11 12 13 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 44 45 46 47 48 49 50 51 53 54 55 56 {
		use "Raw Data/Weather Data/`y'/state`x'.dta", clear
		merge m:1 gridNumber using "Raw Data/Weather Data/linkGridnumberFIPS.dta"
		keep if _m == 3
		drop _m

		* Construct relevant temporal measures *
		egen day = group(dateNum)
		gen month = month(dateNum)
		gen year = year(dateNum)

		* Create 24hr average temperature
		egen tMean = rmean(tMax tMin)

		** Collapse data to the cell x month x year level **
		collapse (mean) tMean tMax (sum)prec, by(gridNumber fips month year) 

		** Collapse data to the county x month x year level **
		collapse (mean) tMean prec, by(fips month year) 

		save "Raw Data/Weather Data/`y'/state_`x'_`y'_monthly.dta", replace
}
}
}

** Append all states so that there is one data set for each year **
forvalues y = 1991(1)2016{
	use "Raw Data/Weather Data/`y'/state_1_`y'_monthly.dta", clear
	foreach x in 4 5 6 8 9 10 11 12 13 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 44 45 46 47 48 49 50 51 53 54 55 56 {
		append using "Raw Data/Weather Data/`y'/state_`x'_`y'_monthly.dta"
	}
	save "Raw Data/Weather Data/`y'/all_states_`y'_monthly.dta", replace
}

** Append all years so that there is one data set for all counties in the US between 1991 and 2016 **
use "Raw Data/Weather Data/1991/all_states_1991_monthly.dta", clear
forvalues y = 1991(1)2016{
	append using "Raw Data/Weather Data/`y'/all_states_`y'_monthly.dta"
}
duplicates drop fips month year, force

egen my = group(year month)

save "Processed Data/US_weather_1991_2016_monthly.dta", replace
